home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / prolog.arc / PROLOG.DOC < prev    next >
Text File  |  1985-04-30  |  49KB  |  1,732 lines

  1.  
  2.  
  3.  
  4.          
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.                            A.D.A PROLOG Documentation 
  31.                   for the Educational and Public Domain Versions
  32.  
  33.               Copyright Robet Morein and Automata Design Associates
  34.  
  35.                                  1570 Arran Way
  36.                                Dresher, Pa. 19025
  37.  
  38.                            Technical:  (215)-646-4894
  39.                            Orders:     (215)-355-5400
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.                                         1
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                 Copyright Notice
  71.  
  72.  
  73.              Please be aware of the following:
  74.  
  75.         There are two A.D.A. PROLOG products distributed on this disk. 
  76.  
  77.              The  public domain PD PROLOG system has been contributed  to 
  78.         the  public domain for unrestricted use with one  exception:  the 
  79.         object  code  may not be  disassembled  or  modified.  Electronic 
  80.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  81.         software the widest possible distribution.
  82.  
  83.              The second version is ED PROLOG,  an enhanced version of  PD 
  84.         PROLOG.  It is not in the public domain. If you have purchased ED 
  85.         PROLOG,  you  have a license to make as many backup copies as you 
  86.         wish.  Please  understand that unauthorized distribution of  this 
  87.         software  will  result  in even less food on the  table  here  at 
  88.         Automata Design Associates.
  89.  
  90.         This  documentation  may be reproduced freely,  but  may  not  be 
  91.         included  in any other document without the written permission of 
  92.         the author, which you probably will get. 
  93.  
  94.  
  95.          
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.                                         2
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.                                   Introduction
  138.                 
  139.  
  140.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  141.         afford  you exposure to THE fifth generation language at the cost 
  142.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  143.         explicable:  We  want you to think of Automata Design  Associates 
  144.         for  fifth  generation  software.  It also gives us a  nice  warm 
  145.         feeling.
  146.              The  memory  requirement  is 192 k.  DOS or  MSDOS  2.0  are 
  147.         required. There are no features requiring IBM PC architecture.
  148.  
  149.  
  150.         Products by Automata Design Associates
  151.  
  152.              There  are five versions of PROLOG available  from  Automata 
  153.         Design Associates.
  154.  
  155.  
  156.              .Public Domain PROLOG
  157.  
  158.         This  serves to further the general awareness of the public about 
  159.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  160.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  161.         Mellish  in  the book Programming In PROLOG (1)  is  implemented. 
  162.         Trace predicates and I/O redirection are not.
  163.              Memory space is deliberately restricted. We are not angels.
  164.  
  165.  
  166.              .Educational PROLOG
  167.  
  168.         At  extremely modest cost this affords an educational institution 
  169.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  170.         available  programming  area  available  within  the  8086  small 
  171.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  172.         a program as it runs.  User settable spy points selectively allow 
  173.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  174.         gives some file ability.
  175.              An  "exec"  function allows the execution of  a  program  or 
  176.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  177.         environment.
  178.              
  179.              Currently the cost of Educational PROLOG is $29.95.
  180.  
  181.  
  182.  
  183.  
  184.  
  185.         _____
  186.         1.  "Programming  in Prolog",  by Clocksin and Mellish,  Springer 
  187.         Verlag,  Berlin-Heidelberg-New York, 1981. This is the definitive 
  188.         Prolog reference. Second edition 1984 has a superior presentation 
  189.         but omits some material.
  190.  
  191.  
  192.  
  193.                                         3
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.              .SMF PROLOG
  204.  
  205.              A  small  increment  in price adds full random  access  file 
  206.         capability. Character and structure I/O are allowed.
  207.              The  "asserta and "assertz" predicates are expanded and work 
  208.         with a clause indexing ability.  One can assert clauses  anywhere 
  209.         in the database under precise pattern matching control. 
  210.              The cost of FSM PROLOG is $49.95
  211.  
  212.              .SMV PROLOG -- Virtual Memory
  213.  
  214.  
  215.              At reasonable cost the addition of virtual memory gives  an 
  216.         expansion of capabilities of an order of magnitude. 
  217.  
  218.              The  database on disk is treated transparently.  No  special 
  219.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  220.         databases  may be mixed.  A unique updating algorithim  preserves 
  221.         the format of the database as typed by the user while making only 
  222.         those changes necessary to make it equivalent to the database  in 
  223.         central memory.
  224.  
  225.              The cost of SVM PROLOG is $99.95
  226.  
  227.  
  228.              .IVM PROLOG
  229.  
  230.              At  the  cost of some speed these versions allow the use  of 
  231.         several  hundred kilobytes of central memory for  clause  storage 
  232.         and 64K bytes of stack.
  233.              Free of the constraints of the 8086 small memory model, it 
  234.         is our intent that the entire library of the Computer Innovations 
  235.         "C" compiler will be brought out in clausal form.
  236.  
  237.              Virtual  memory is of course included.  The cost is  a  mere 
  238.         $250.
  239.  
  240.              .LVM PROLOG
  241.  
  242.              For  specialized applications,  a version is available  that 
  243.         accesses the full 1 megabyte address space of the 8086.
  244.  
  245.              The cost is $500.
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.                                         4
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                               To Place Your Order:
  269.  
  270.              The phone for orders is (215)-355-5400.  Visa, Mastercharge, 
  271.         and American Express are accepted.
  272.  
  273.  
  274.                               Technical Information
  275.  
  276.              Technical information may be obtained at (215) - 646- 4894
  277.  
  278.              Perhaps we can answer the following questions in advance:
  279.  
  280.              There is no support for:  APPLE II, Atari, Commodore, or  
  281.         CPM 80 .  Other machines from these manufactures may be supported 
  282.         in the future.
  283.  
  284.              The   public   domain   version  is   available   from   the 
  285.         organizations  who distribute such software.  It is not available 
  286.         from us.
  287.              The MSDOS products are available on 5" and 8" diskettes.
  288.              
  289.  
  290.                                      Returns
  291.  
  292.              The software may be returned within 60 days of purchase  for 
  293.         a full refund.  This applies whether or not the software has been 
  294.         used.  We do ask that manuals, disks and packaging be returned in 
  295.         excellent condition.
  296.  
  297.                                Other Environments
  298.  
  299.              Various other machines are targeted for support. The code is 
  300.         written in "C", so it is fairly easy to move.
  301.              
  302.              If  you are a hardware manufacturer and you would like A.D.A 
  303.         PROLOG  to be available for your hardware,  contact us at  (215)-
  304.         646-4894.
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.                                         5
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                       How to run the Demonstration Programs 
  335.                         without Knowing What You're Doing
  336.  
  337.              We strongly advise that you purchase the book Programming in 
  338.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  339.         For  the  impatient we give some advice.  Type the  demonstration 
  340.         program you wish to run.  There must be at least one entry  point 
  341.         within  the  program.  
  342.  
  343.         Note:  Please  understand that these are demonstrations programs. 
  344.         Regarding  user  interface,  they are poorly  written.  You  will 
  345.         probably have to read Clocksin and Mellish to appreciate that the 
  346.         following examples of input are not equivalent: "yes." , "yes" .
  347.  
  348.  
  349.         The animals program - "animal"
  350.          
  351.              Most  of the examples require C & M for  comprehension.  The 
  352.         program "animals", however, can be appreciated by anyone. It is a 
  353.         traditional   example of an expert system.
  354.              
  355.              Run the prolog.exe file.  The prompt "?-" will appear.  Type 
  356.         "consult( 'animals' ).<CR>".  Here <CR> indicates you are to type 
  357.         a  carriage  return.  The PROLOG system will load  "animals"  and 
  358.         compile  it into an internal form.  When the "?-" prompt  appears 
  359.         PROLOG is ready to run the "animals" guessing game. The object of 
  360.         the program is to deduce the animal you are thinking of. To start 
  361.         it  off  type  "help.<CR>".  PROLOG  will  respond  by  asking  a 
  362.         question. 
  363.              Because of the way the animals program is written,  you must 
  364.         respond in a rigid format.  You may type "yes<CR>",  "no<CR>", or 
  365.         "why<CR>". 
  366.              Eventually the program will terminate with either a guess as 
  367.         to what animal you are thinking of,  or a remark that the  animal 
  368.         is  not within its domain of knowledge.  The program has learned, 
  369.         however.  You  may  type  "help.<CR>" again to  see  what  effect 
  370.         additional knowledge has on the program's behavior.  You may also 
  371.         cause it to forget what it has learned by typeing 
  372.           "forget( user ).<CR>".
  373.              
  374.  
  375.         The Hematology Diagnosis Program - "hemat"
  376.  
  377.              Although  the  logical structure is not as sophisticated  as 
  378.         that of "animals", it is interesting for several reasons:
  379.  
  380.              1)  The  program  evaluates numerical data to  arrive  at  a 
  381.         diagnosis.
  382.  
  383.              2) Although inaccurate, it demonstrates that useful question 
  384.         answering systems are not difficult to write in PROLOG.
  385.  
  386.              3)  There  are  some mistakes in  the  program,  which  only 
  387.         slightly impede its usefulness. 
  388.  
  389.  
  390.  
  391.                                         6
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.              This  program  uses  structure  input.  Terminate  all  your 
  401.         answers with a period, as in "y.<CR>", or "no.<CR>".
  402.  
  403.              The starting point is "signs.<CR>".  PROLOG will prompt  you 
  404.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  405.         varieties of a hemolytic anemia.
  406.              The program could use a good working over by a  hematologist 
  407.         and we would be delighted to collaborate.
  408.  
  409.  
  410.         Prime Number Generator - "sieve"
  411.  
  412.              This  program demonstrates that anything can be programed in 
  413.         PROLOG if one tries hard enough.  Asking the  question   
  414.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  415.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  416.         exhausts the stack space of the small model interpreters.
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                                         7
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                              Running the Interpreter
  467.  
  468.         COMMANDS: Give commands in lower case.
  469.  
  470.         TO RUN:
  471.              Invoke PROLOG.EXE. After the "?-" prompt appears,
  472.                type "consult( <filename><CR> )", where <filename> is the 
  473.                desired database.
  474.              To exit, type "exitsys.<CR>"
  475.  
  476.         TO ASK A QUESTION:
  477.              At the prompt, type "<expression>.<CR>", where 
  478.              <expression>  is  a  question as described by  Clocksin  and 
  479.              Mellish. Be sure to terminate the question with a period.
  480.              The question may be up to 500 characters long.
  481.  
  482.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  483.              The structure may be up to 500 characters in length. Be sure 
  484.              to terminate with a period.
  485.  
  486.         TO ASK FOR ANOTHER SOLUTION:
  487.              If a solution has been provided, the PROLOG interpreter will 
  488.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  489.              interpreter perform a search.
  490.  
  491.         TO ABORT A SEARCH:
  492.              Simply   type   the  escape  key.   The   interpreter   will      
  493.              respond  with  "Interrrupted.",  and return to  the  command      
  494.              prompt.
  495.  
  496.         TO LOAD ANOTHER DATABASE:
  497.              Type "consult(<filename>).<CR>"
  498.  
  499.         TO REMOVE A DATABASE:
  500.              Type "forget(<filename>).<CR>"
  501.  
  502.         TO EXIT TO THE OPERATING SYSTEM:
  503.              Type "exitsys.<CR>"
  504.  
  505.              The  system totally interactive;  any commands the  operator 
  506.         gives  are and must be valid program statements.  Statements must 
  507.         terminate with a period.  
  508.              All commands which take a file name also accept a path name. 
  509.         Any  name which is not a valid PROLOG atom (refer to C & M)  must 
  510.         be enclosed in single quotes. Thus one could say
  511.  
  512.              consult( expert )
  513.  
  514.         but one would need single quotes with
  515.  
  516.              consult( 'b:\samples\subtype\expert' ).
  517.  
  518.  
  519.         To exit the system, type "exitsys.<CR>"
  520.  
  521.  
  522.  
  523.                                         8
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  533.         quotes, ie.,  '\b:\samples\animal' .
  534.  
  535.         You may consult more than one file at a time.  However, all names 
  536.         are public and name conflicts must be avoided. The order in which 
  537.         modules are loaded may,  in cases of poor program design,  affect 
  538.         the behavior.
  539.  
  540.                              Syntactical Differences
  541.  
  542.              There   are  very  few   syntactical   differences,   mostly 
  543.         unrecognized and/or minor.
  544.              When  an operator is declared using the "op" statement,  the 
  545.         operator must be enclosed in single quotes in the "op"  statement 
  546.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  547.         Subsequently,  however,  the parser will recognize it for what it 
  548.         is,  except  in  the "unop" statement,  where it  must  again  be 
  549.         enclosed in single quotes.
  550.  
  551.              Variable numbers of functor paramaters is supported.
  552.  
  553.              A  goal  may be represented by a  variable,  which  is  less 
  554.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  555.         functors.  The  variable must be instantiated to a  functor  when 
  556.         that goal is pursued.
  557.  
  558.              Rules which appear inside other expressions must be enclosed 
  559.         in parenthesis if the "," operator is to be recognized as logical 
  560.         connectives.
  561.  
  562.              All  operators described by C & M,  and user defined  infix, 
  563.         prefix,  and  postfix operators with variable  associativity  and 
  564.         precedence  are supported exactly as in C & M.  Please note  that 
  565.         operators  must  be enclosed in single quotes when  declared  and 
  566.         removed.
  567.  
  568.              A name may be up to 250 characters long and may contain the
  569.         embedded escape sequences \n,  \r, and \t if the name is enclosed 
  570.         in single quotes.
  571.  
  572.  
  573.                                    Arithmetic
  574.  
  575.              Signed  integer  arithmetic  is  supported.   The  range  is 
  576.         -2  exp( 31 ) to +2 exp( 31 ),  or approximately -1073741800 to + 
  577.         1073741800.  Integers  may be used as file pointers (model FL and 
  578.         up only.)
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                         9
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                          The Built In Predicate Library
  599.  
  600.  
  601.              The  following built in predicates described by Clocksin and 
  602.         Mellish   are  supported.   Those  marked  with  asterisks   have 
  603.         additional features:                                     
  604.              ?-                                        mod       
  605.              arg                                       name      
  606.              asserta                                   nl        
  607.              assertz                                   nonvar    
  608.              atom                                      not       
  609.              atomic                                    put       
  610.              call                                      read      
  611.              clause                                    repeat    
  612.              consult                                   retract   
  613.              !                                         tab       
  614.             *display                                   true      
  615.              functor                                   var       
  616.              get0                                     *write     
  617.              integer                                 
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.                                        10
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                         Description of the Modifications.
  665.              
  666.              
  667.         display-
  668.         put-
  669.         write
  670.  
  671.              The  functions  "put",  "write",  and  "display"  have  been 
  672.         modified to accept multiple arguments.  Thus,  "put( a,  b,  c )" 
  673.         would result in the display of "abc".
  674.  
  675.  
  676.              The  following built in predicates have been added by A.D.A. 
  677.         and support additional features.
  678.  
  679.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  680.         can contain the escape sequences
  681.  
  682.              '\n', '\r', '\t' and '\''.
  683.  
  684.              If these atoms are printed by "display" or "write" they  are 
  685.         printed  just  as they are.  If they are printed by  the  "print" 
  686.         clause they are translated as follows:
  687.  
  688.         '\n'  results  in  the printing of a carriage return and  a  line 
  689.         feed.
  690.         '\r' results in the printing of a carriage return only.
  691.         '\t' results in the printing of a tab character.
  692.         '\'' allows the printing of a single quote within a quoted atom.
  693.  
  694.              The "portray" feature is not presently implemented.
  695.          
  696.                         Description of the New Predicates
  697.  
  698.  
  699.         clearops-
  700.              Nullify  the  operator  status  of  every  operator  in  the 
  701.              database.
  702.  
  703.         concat( (<variable> | <functor>), <List> )
  704.              A  list  of functors or operators is concatenated  into  one 
  705.         string,  which becomes the name of a new atom to which <variable> 
  706.         or <functor> must match or be instantiated.
  707.  
  708.  
  709.         dir( option )
  710.              Provide  an  alphabetized listing to the console  of  atoms, 
  711.         constants,   or   open  files.   Without  options,   simply  type 
  712.         "dir.<CR>". Options are:
  713.  
  714.              dir( pred ) - list clause names only.
  715.              dir( files ) - list open files only.
  716.  
  717.  
  718.         exitsys
  719.  
  720.  
  721.                                        11
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.              Exit to the operating system.
  731.  
  732.         forget( <file name> )
  733.              Make  a database unavailable for use and reclaim the storage 
  734.         it occupied.
  735.  
  736.  
  737.         ratom( <arg>, <stream> )-
  738.              Read an atom from the input stream,  to which <arg>  matches 
  739.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  740.         given, the input stream defaults to the standar input.
  741.              Input is terminated by a CR or LF, which are not included in 
  742.         the stream.
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.                                        12
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.                                  Prolog Tutorial          
  798.  
  799.  
  800.                                   Introduction
  801.  
  802.  
  803.  
  804.  
  805.              Probably  you  have heard of the language PROLOG within  the 
  806.         last year or so. You probably wondered the following things:
  807.  
  808.         1) What does the name stand for? Names of computer languages are 
  809.         almost always acronyms.
  810.  
  811.         2) What is it good for?
  812.  
  813.         3) Why now?
  814.  
  815.         4) Can I get a copy to play with?
  816.  
  817.              Congratulations! You obviously know the answer to the fourth 
  818.         question. We now respond to the other three.
  819.              
  820.         1)  The  name  stands for "programming in logic." This  we  shall 
  821.         elaborate on in depth later on.
  822.  
  823.         2) PROLOG is good for writing question answering systems.  It  is 
  824.         also   good   for  writing  programs  that  perform   complicated 
  825.         strategies  that  compute the best or worst way to  accomplish  a 
  826.         task, or avoid an undesirable result.
  827.  
  828.         3) PROLOG was virtually unknown in this country until researchers 
  829.         in  Japan announced that it was to be the core language  of  that 
  830.         country's fifth generation computer project.  This is the project 
  831.         with  which  Japan hopes to achieve a domainant position  in  the 
  832.         world information industry of the 1990's. 
  833.  
  834.              PROLOG  is  one of the most unusual computer languages  ever 
  835.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  836.         BASIC.  The facilities complement,  rather than replace those  of 
  837.         conventional  languages.  Although  it  has great  potential  for 
  838.         database  work,  it  has  nothing in  common  with  the  database 
  839.         languages used on microcomputers.
  840.  
  841.              Perhaps  the  best point to make is that while  conventional 
  842.         languages are prescriptive, PROLOG is descriptive. A statement in 
  843.         a conventional language might read:
  844.  
  845.              if( car_wheels = TRUE ) then
  846.                begin
  847.                  (some sort of procedure)
  848.                  X = X + 1;
  849.                end 
  850.  
  851.  
  852.  
  853.                                        13
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.         A statment in PROLOG could just be a statment of fact about  cars 
  863.         and wheels. There are many relationships that hold. For instance,
  864.  
  865.              has( car, wheels ).
  866.  
  867.              has( car, quant(wheels, four) ).
  868.  
  869.              round( wheels ).
  870.  
  871.         Each  of  these statments is an independent fact  relating  cars, 
  872.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  873.         independent, they can be put into a PROLOG program by programmers 
  874.         working separately. The man who is a specialist on car bodies can 
  875.         say  his thing,  the wheel specialist can have his say,  and  the 
  876.         participants can work with relative independence. And this brings 
  877.         to light a major advantage of PROLOG:
  878.  
  879.  
  880.                              PARALLEL PROGRAMMING!!!
  881.                             
  882.  
  883.         With  conventional  programming languages projects can  still  be 
  884.         "chunked",  or  divided between programmers.  But efficiency on a 
  885.         team  project  drops  drastically below that  of  the  individual 
  886.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  887.         participants    grows   the   need   for   communication    grows 
  888.         geometrically. The time spent communicating can exceed that spent 
  889.         programming! 
  890.              Although   PROLOG   does   not  eliminate   the   need   for 
  891.         task  coordination,  the problem is considerably  simplified.  It 
  892.         also provides the ability to answer questions in a "ready to  eat 
  893.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  894.         statements about cars and wheels previously given,  could you ask 
  895.         it the following question?   
  896.  
  897.                        
  898.               has( car, X ), round( X ).
  899.  
  900.              Does  a  car  have anything which  is  round?  The  question 
  901.         instructs the PROLOG interpreter to consider all the objects that 
  902.         it  knows are possessed by a car and find those which are  round. 
  903.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  904.         of a smart database searcher.  It can not only find the facts but 
  905.         selectively find them and interpret them.
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.                                        14
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.              Consider the problem of a fault tree, as exemplified by this 
  929.         abbreviated one:
  930.  
  931.  
  932.  
  933.         {Car won't start}
  934.              | 
  935.              | 
  936.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  937.                 (Yes)                                       v
  938.                  |                                  {Check battery}
  939.                  |
  940.         [Smell gasoline](yes) --> {Try full throttle cranking}
  941.                  |                       (failure)
  942.         /--------/                           |
  943.  
  944.                             (details omitted)
  945.  
  946.  
  947.  
  948.              The fault tree is easily programmed in BASIC. Later we shall 
  949.         show  that  PROLOG supplies a superior replacement for the  fault 
  950.         tree.  Though the tree is capable of diagnosing only the  problem 
  951.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  952.         appropriate  tree from facts and rules you have provided.  PROLOG 
  953.         is not limited to answering one specific question.  Given  enough 
  954.         information,  it  will attempt to find all deductive solutions to 
  955.         any problem.
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.                                        15
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.                                   PROLOG PRIMER
  996.  
  997.         I.                       Rules and Facts     
  998.  
  999.  
  1000.  
  1001.              This  is  where you should start if you know  nothing  about 
  1002.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  1003.  
  1004.         1)   has( car, wheels ).
  1005.  
  1006.         This  statement  is a "fact.  The word "has" in this statment  is 
  1007.         known  either  as a functor or predicate.  It is a name  for  the 
  1008.         relationship  within the parenthesis.  It implies that a car  has 
  1009.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  1010.         arbitrary and established by you. You could just as easily say:
  1011.  
  1012.         2)   has( wheels, car ).
  1013.  
  1014.         and if you wrote this way consistently,  all would be  well.  The 
  1015.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  1016.         "car" are constants. 
  1017.              
  1018.         A   database   of  facts  can  illustrate  the   data   retrieval 
  1019.         capabilities of PROLOG. For instance:
  1020.  
  1021.         3)   has( car, wheels ).
  1022.              has( car, frame ).
  1023.              has( car, windshield ).
  1024.              has( car, engine ).
  1025.  
  1026.         You could then ask PROLOG the question:
  1027.  
  1028.         4)   has( car, Part ).
  1029.  
  1030.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  1031.         will make Part equal to whatever constant is required to make the 
  1032.         question match one of the facts in the database. Thus PROLOG will 
  1033.         respond:
  1034.  
  1035.              Part = wheels.
  1036.              
  1037.              More?(Y/N):
  1038.  
  1039.         If you type "y" the next answer will appear:
  1040.  
  1041.              Part = frame.
  1042.  
  1043.              More?(Y/N):
  1044.  
  1045.         If you continue, PROLOG will produce the answers Part = windshield 
  1046.         and Part = engine. Finally, you will see:
  1047.  
  1048.              More?(Y/N):y
  1049.  
  1050.  
  1051.                                        16
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.              No.
  1062.          
  1063.         indicating that PROLOG has exhausted the database.  Incidentally, 
  1064.         when  a  variable is set equal to a constant or  other  variable, 
  1065.         it is said to be instantiated to that object.
  1066.  
  1067.              Notice  that  PROLOG searches the database forwards  and  in 
  1068.         this case,  from the beginning.  The forward search path is built 
  1069.         into PROLOG and cannot be changed. An author of a program written 
  1070.         in  a  prescriptive language is quite conscious of the  order  of 
  1071.         execution  of  his program,  while in PROLOG it is  not  directly 
  1072.         under his control.
  1073.              
  1074.              The other major element is the rule which is a fact which is 
  1075.         conditionally true. In logic this is called a Horn clause: 
  1076.  
  1077.  
  1078.         5)   has( X, wheels ) :- iscar( X ).
  1079.  
  1080.         The  fact iscar( car ) and the above rule are equivalent to
  1081.  
  1082.         6)   has( car, wheels).
  1083.  
  1084.         The  symbol :- is the "rule sign." The expression on the left  of 
  1085.         :-is the "head" and on the right is the body.  The variable X has 
  1086.         scope  of the rule,  which means that it has meaning only  within 
  1087.         the rule.  For instance,  we could have two rules in the database 
  1088.         using identically named variables.
  1089.  
  1090.  
  1091.         7)   has( X,  transportation ) :- 
  1092.                            has( X,  car ), has( license, X ).
  1093.  
  1094.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  1095.  
  1096.         The  variables  X in the two expressions are completely  distinct 
  1097.         and have nothing to do with each other.
  1098.  
  1099.         The comma between has( X, car ) and has( license, X ) means "and" 
  1100.         or logical conjuction.  The rule will not be true unless both the 
  1101.         clauses has(X, car) and has( license, X ) are true.
  1102.  
  1103.  
  1104.              On the other hand if there is a rule
  1105.              
  1106.         9)   has( license, X ) :- passedexam( X ).
  1107.  
  1108.         consider what PROLOG will do in response to the question:
  1109.  
  1110.         10)  has( harry, transportation ).
  1111.  
  1112.         (Notice  that  harry has not been capitalized because we  do  not 
  1113.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  1114.         enclosed in single quotes.)
  1115.  
  1116.  
  1117.                                        17
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.              It  will scan the database and use (7),  in which X will  be 
  1128.         instantiated to harry. The rule generates two new questions:
  1129.  
  1130.         11)  has( harry, car ).
  1131.  
  1132.         12)  has( license, harry ).
  1133.  
  1134.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  1135.         satisfied and the database is scanned for a match to (12). PROLOG 
  1136.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  1137.         question is now posed:
  1138.  
  1139.         13)  passedexam( harry ).
  1140.  
  1141.              If there is a fact:
  1142.  
  1143.              passedexam( harry ).
  1144.  
  1145.         in  the database then all is well and harry  has  transportation. 
  1146.         If there is not, then PROLOG will succinctly tell you:
  1147.  
  1148.              No.
  1149.  
  1150.         But  suppose Harry has money and can hire a chauffer as any  good 
  1151.         programmer  can.  That  could be made part of the program in  the 
  1152.         following way.
  1153.  
  1154.              The rule which PROLOG tried to use was:
  1155.  
  1156.         14)  has( X,  transportation ) :- 
  1157.                            has( X,  car ), has( license, X ).
  1158.  
  1159.         At any point following it there could be included another rule:
  1160.  
  1161.         15)  has( X, transportation ) :- has( X, money ).
  1162.  
  1163.         or simply the bald fact:
  1164.  
  1165.         16)  has( harry, transportation ).
  1166.  
  1167.              These  additional  rules  or  facts would  be  used  in  two 
  1168.         circumstances.  If at any point a rule does not yield a solution, 
  1169.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  1170.         applicable  one.  This process is known as "backtracking  search" 
  1171.         and can be quite time consuming.
  1172.  
  1173.  
  1174.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  1175.         search  for an additional distinct solution.  It will attempt  to 
  1176.         find an alternate rule or fact for the last rule or fact used. If 
  1177.         that  fails,  it  will back up to the antecedent rule and try  to 
  1178.         find  an alternate antecedent.  And it will continue to  back  up 
  1179.         until  it  arrives at the question you asked,  at which point  it 
  1180.         will say:
  1181.  
  1182.  
  1183.                                        18
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.              No.
  1194.  
  1195.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  1196.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  1197.         question (16).
  1198.  
  1199.  
  1200.  
  1201.  
  1202.         II.                          Grammar
  1203.  
  1204.              It is a boring subject, but it must be discussed. All PROLOG 
  1205.         statements are composed of valid terms, possibly a rule sign (":-
  1206.         "),  commas representing conjunction ("and"), and a period at the 
  1207.         very end.
  1208.              A term is a structure, constant, variable, or number.
  1209.          
  1210.              What is a structure? It is a kind of grouping:
  1211.  
  1212.              1) Structures consist of a functor, and a set of objects or
  1213.                 structures in parenthesis.
  1214.  
  1215.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  1216.                 which we have not discussed yet.
  1217.  
  1218.              3)  A  constant or functor must be a string of  letters  and 
  1219.                 numbers, beginning with a lower case letter, unless
  1220.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  1221.                 pardner'  ),  in  which  case you are  freed  from  these 
  1222.                 restrictions.
  1223.              4) A  variable  must be a string of  letters  and  numbers 
  1224.                 beginning with a capital letter.
  1225.              
  1226.              5) A  functor  may optionally have  arguments  enclosed  in 
  1227.                 parenthesis , as in: hascar( X ) or hascar. 
  1228.  
  1229.         An example:  "has( X, transportation )." is a structure.
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.                                        19
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.         III.                     Input / Output      
  1259.  
  1260.              You   now   know  enough  to  write  simple  databases   and 
  1261.         interrogate   them  profitably.   But  before  we  examine   more 
  1262.         sophisticated  examples,  it  will be necessary to add input  and 
  1263.         output to the language. There are built in functions which appear 
  1264.         as rules which are satisfied once. Thus the statment:
  1265.  
  1266.              write( 'Hello world.' ).
  1267.  
  1268.         can be included on the right side of a rule:
  1269.  
  1270.  
  1271.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  1272.         can  also write "write( X )" where X is some variable.  Note that 
  1273.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  1274.         which denote a constant, are required. Double quotes would denote 
  1275.         a list, which is another thing entirely.
  1276.  
  1277.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  1278.         function  "write"  is  executed and the message  printed  to  the 
  1279.         screen.
  1280.              The  builtin  read( X ) reads a "structure" that  you  input 
  1281.         from the keyboard. More formally, we have
  1282.  
  1283.              read( <variable> or <constant> )
  1284.              write( <variable> or <constant> )
  1285.  
  1286.         If you write read( Input ),  then the variable "keyboard" will be 
  1287.         assigned to whatever is typed at the keyboard,  provided that the 
  1288.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  1289.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  1290.         is a constant,  and the user at the keyboard did not type exactly 
  1291.         "baloney." 
  1292.  
  1293.         When you input a structure in response to a "read" statement,  be 
  1294.         sure to end it with a period and an <ENTER>. 
  1295.  
  1296.              There  is  a convenient way of putting the cursor on  a  new 
  1297.         line. This is the builtin "nl". For example:
  1298.  
  1299.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  1300.  
  1301.         would result in:
  1302.  
  1303.              line 1
  1304.              line 2
  1305.  
  1306.              There  is  also a primitive form of input/output for  single 
  1307.         characters. It will be discussed later.
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.                                        20
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.         IV.                   A Fault Tree Example
  1325.  
  1326.              Consider the "won't start" fault tree for an automobile:
  1327.  
  1328.         {Car won't start}
  1329.              | 
  1330.              | 
  1331.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  1332.                 (Yes)                                       v
  1333.                  |                                  {Check battery}
  1334.                  |
  1335.         [Smell gasoline](yes) --> {Try full throttle cranking}
  1336.                  |                       (failure)
  1337.         /--------/                           |
  1338.         |           /------------------------/ 
  1339.         |           |                       
  1340.         |           |
  1341.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  1342.         |          (no)
  1343.         |           |
  1344.         |           |
  1345.         |  [Check for defective carburator](yes)--\
  1346.         |          (no)                           v
  1347.         |                                {Repair carburator}
  1348.         \----\
  1349.              |
  1350.              |
  1351.         [Is spark present](no)-->[Do points open and close](no)-\
  1352.              |                             (yes)                v
  1353.         /----/                               |          {Adjust points}
  1354.         |           /------------------------/
  1355.         |           |
  1356.         |  [Pull distributor wire, observe spark](blue)--\
  1357.         |        (orange)                                v
  1358.         |           |                           {Check plug wires & cap}
  1359.         |           |
  1360.         |  [Measure voltage on coil primary](not 12V)--\
  1361.         |         (12V)                                v
  1362.         |           |              {Check wiring, ballast resistor}
  1363.         |           |
  1364.         |  [Check condenser with ohmmeter](conducts)--\
  1365.         |    (no conduction)                          v
  1366.         |           |                         {Replace condenser}
  1367.         |           |
  1368.         |  [Open and close points](voltage not 0 - 12)--\
  1369.         |   (voltage swings 0 - 12)                     v
  1370.         |           |                         {Fix primary circuit}
  1371.         |           |
  1372.         |  {Consider hidden fault, swap components]
  1373.         |
  1374.         |
  1375.         \-------{Call a tow truck!!}
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.                                        21
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.              A PROLOG program to  implement this is simple. Each statment 
  1391.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  1392.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  1393.         solution. 
  1394.  
  1395.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  1396.                             affirm, nl,
  1397.                             write( 'Check battery' ).
  1398.  
  1399.         'car wont start' :- write( 'Smell gasoline?' ), 
  1400.                             affirm, nl,
  1401.                             'fuel system'.
  1402.  
  1403.         'fuel system'    :- write( 'Try full throttle cranking' ).
  1404.  
  1405.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  1406.                             affirm, nl,
  1407.                             write( 'Replace fuel line.' ).
  1408.  
  1409.         'fuel system'    :- write( 'Check carburator' ).
  1410.  
  1411.         'car wont start' :- write( 'Is spark present?' ),
  1412.                             not( affirm ), nl,
  1413.                             'no spark'.
  1414.  
  1415.         'no spark'       :- write( 'Do points open and close?' ),
  1416.                             not( affirm ), nl,
  1417.                             write( 'Adjust or replace points.' ).
  1418.  
  1419.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  1420.                             affirm,
  1421.                             write( 'Check plug wires and cap.' ).
  1422.  
  1423.         'no spark'       :- write( 'What is the voltage on the primary
  1424.                              of the coil: ' ), 
  1425.                             read( Volts ), 
  1426.                             Volts < 10,
  1427.                             nl,
  1428.                             write('Check wiring and ballast resistor.').
  1429.  
  1430.         'no spark'       :- write( 'Does the capacitor leak?' ),
  1431.                             affirm,
  1432.                             write( 'Replace the capacitor.' ).
  1433.  
  1434.         'no spark'       :- not( 'primary circuit' ).
  1435.  
  1436.         'primary circuit' 
  1437.                          :- write( 'Open the  points.  Voltage  across 
  1438.                               coil?:'), nl,
  1439.                             read( Openvolts ), Openvolts < 1, 
  1440.                             write(  'Close the points.  Voltage  across 
  1441.                               coil?:'),
  1442.                             read( Closevolts ), Closevolts > 10, nl,
  1443.                             write( 'Primary circuit is OK.' ). 
  1444.  
  1445.  
  1446.  
  1447.                                        22
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.         'no spark'       :- write( 'Consider a hidden fault. Swap
  1457.                               cap, rotor,points,capacitor.' ).
  1458.  
  1459.  
  1460.         'Car wont start' :- write( 'Get a tow truck!!' ).
  1461.  
  1462.  
  1463.                                  --End program--
  1464.  
  1465.  
  1466.              The  above  is  a  simple example of  an  expert  system.  A 
  1467.         sophisticated  system would tell you exactly the method by  which 
  1468.         it  has reached a conclusion.  It would communicate by a  "shell" 
  1469.         program  written  in PROLOG which would accept a wider  range  of 
  1470.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  1471.         interpreter directly.
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.                                        23
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.         V.                            Lists               
  1523.  
  1524.              Consider  a  shopping list given you by your wife.  It is  a 
  1525.         piece of paper with items written on it in an order that probably 
  1526.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  1527.         followed by carrots, hamburger, and finally a flea collar for the 
  1528.         dog, if you can find one. In PROLOG such a list would be written:
  1529.  
  1530.         1)   [eggs, carrots, hamburger, fleacollar]
  1531.  
  1532.         The  order of a list is important so that eggs and carrots cannot 
  1533.         be reversed and PROLOG be uncaring.
  1534.  
  1535.         Let us put the list in a structure:
  1536.  
  1537.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  1538.  
  1539.         Then  if you wished to isolate the head of the list you could ask 
  1540.         the question:
  1541.  
  1542.              shopping( [ Mostimportant | Rest ] ).
  1543.  
  1544.         and PROLOG would respond:
  1545.  
  1546.              Mostimportant   =  eggs,   
  1547.              Rest   =   [carrots,   hamburger, fleacollar].
  1548.  
  1549.         The vertical bar "|" is crucial here. It is the string extraction 
  1550.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  1551.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  1552.         separate the head of the list from the rest, or tail.
  1553.  
  1554.  
  1555.              You  may have gained the impression that PROLOG is a  rather 
  1556.         static language capable of answering simple questions,  but it is 
  1557.         far  more powerful than that.  The string extraction operator  is 
  1558.         the  key.  It permits PROLOG to whittle a complex expression down 
  1559.         to the bare remainder.  If the rules you have given it permit  it 
  1560.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  1561.         achieved. An example of this is the definition of "append."
  1562.  
  1563.              Let  us suppose you have not yet done yesterday's  shopping, 
  1564.         let alone today's. You pull it out of your wallet and sootch tape 
  1565.         it to the list your wife just gave you. Yesterday's list was:
  1566.  
  1567.              [tomatoes, onions, ketchup]
  1568.  
  1569.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  1570.  
  1571.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  1572.  
  1573.         To  take one list and to attach it to the tail of another list is 
  1574.         to  "append"  the first to the second.  The PROLOG definition  of 
  1575.         append is:
  1576.  
  1577.  
  1578.  
  1579.                                        24
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590.         Rule1:     append( [], L, L ).
  1591.  
  1592.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  1593.                       append( List1, List2, List3 ].
  1594.  
  1595.         The  general  scheme is this:  
  1596.  
  1597.         The definition consists of one rule and one fact.  The rule  will 
  1598.         be used over and over again until what little is left matches the 
  1599.         fact.  The [] stands for empty list,  which is like a bag without 
  1600.         anything in it. This is an example of a recursive definition.
  1601.              Suppose we ask:
  1602.  
  1603.              append( [a,b,c], [d,e,f], Whatgives ).
  1604.  
  1605.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  1606.         2. Rule 2 is invoked again with arguments:
  1607.              ( [b,c], [d,e,f], List3 ).
  1608.         3. Rule 2 is invoked again with arguments:
  1609.              ( [b], [d,e,f], List3 ).
  1610.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  1611.             matches. End.
  1612.  
  1613.         How does this cause a list to be constructed? The key is to watch 
  1614.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  1615.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  1616.         2. Now lets trace this as rule two is successivly invoked: 
  1617.  
  1618.  
  1619.                 Whatgives                                                
  1620.                    |                                                     
  1621.                    |                                                     
  1622.                    |                                                     
  1623.                    v                                                     
  1624.         Rule2:  [X|List3] (List3 = [b,c])                                
  1625.                  |  \                                                    
  1626.                  |   \                                                   
  1627.                  |    \                                                  
  1628.                  v     \                                                 
  1629.         Rule2:   a   [X'|List3'] (List3' = [c])                          
  1630.                       |    \                                             
  1631.                       |     \                                            
  1632.                       |      \                                           
  1633.                       v       \                                          
  1634.         Rule2:        b     [X''|List3''] (List3'' = [], ie., empty set.)
  1635.                               |    \                                     
  1636.                               |     \                                    
  1637.                               |      \                                   
  1638.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  1639.                                                                          
  1640.         End.
  1641.  
  1642.  
  1643.  
  1644.  
  1645.                                        25
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  1655.         2 never alters List2. It supplies it to whatever rule it invokes. 
  1656.         So L in rule 1 is the original List2, or [a,b,c].
  1657.  
  1658.              This example would not have worked if the order of rules one 
  1659.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  1660.         attempts to use the the first rule encountered. You could imagine 
  1661.         it as always reading your program from the top down in attempting 
  1662.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  1663.         an  unpleasant  thing  would  have happened  if  the  order  were 
  1664.         reversed. The program would loop forever.
  1665.              
  1666.  
  1667.  
  1668.  
  1669.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  1670.         appetite. You should now purchase the book
  1671.  
  1672.              Programming In Prolog
  1673.              W.F. Clocksin and C.S. Mellish
  1674.              Springer - Verlag
  1675.              Berlin,Heidelberg,New York. 1981,1984
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.                                        26
  1712.  
  1713.  
  1714.  
  1715.  
  1716. 
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.                                        26
  1728.  
  1729.  
  1730.  
  1731.  
  1732.